### This script will batch out to the HPC one job for each day from 1970 - 2009
### The result will be two spatial surfaces, one of daily max temp, and one of daily min temp
### Directory where AWAP tmax / tmin / and insolation files are located
### Modified for submission to two processor nodes by batching out 3 months at a time and then waiting 16 minutes between batches

tmax.dir = '/home/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/Tmax2/'
tmin.dir = '/home/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/Tmin2/'
insol.dir = '/home/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/BC6/'

### List the appropriate files in these directories

tmax.files = list.files(tmax.dir, pattern='.asc.gz', recursive=TRUE, full.names=TRUE)
tmin.files = list.files(tmin.dir, pattern='.asc.gz', recursive=TRUE, full.names=TRUE)
insol.files = list.files(insol.dir, pattern='insol', recursive=TRUE, full.names=TRUE)

### Set location of script to run

script.file = '/home/99/jc152199/MicroclimateStatisticalDownscale/250mASCII/brtpredsfinal/spatialpreds_2run_AWAPfromJezza.r'

# Establish directory to write out .sh files to

tmp.pbs = '/home/99/jc152199/tmp.pbs8/'
setwd(tmp.pbs)

### Create a vector to match the Julian day from Insolation ASCII to the proper date

insol.vec = unique(substr(tmax.files,nchar(tmax.files)-10,nchar(tmax.files)-7))

for (yearx in c(1950:1969,2009:2011)) # This loop will create an .sh file with two arguments for each day 

	{
	
	### Subset all files by year
	
	ttmax.files = tmax.files[which(substr(tmax.files,nchar(tmax.files)-14,nchar(tmax.files)-11)==yearx)]
	ttmin.files = tmin.files[which(substr(tmin.files,nchar(tmin.files)-14,nchar(tmin.files)-11)==yearx)]
	
	for (monthx in unique(substr(ttmax.files,nchar(ttmax.files)-10,nchar(ttmax.files)-9)))
		
		{
		
		### Subset files by month
		
		tttmax.files = ttmax.files[which(substr(ttmax.files,nchar(ttmax.files)-10,nchar(ttmax.files)-9)==monthx)]
		tttmin.files = ttmin.files[which(substr(ttmin.files,nchar(ttmin.files)-10,nchar(ttmin.files)-9)==monthx)]
		
		for (dayx in substr(tttmax.files,nchar(tttmax.files)-8,nchar(tttmax.files)-7))
		
			{
			
			### Subset files by day
			### These are the tmax and tmin files to define as arguments
			
			tmax.file = tttmax.files[which(substr(tttmax.files,nchar(tttmax.files)-8,nchar(tttmax.files)-7)==dayx)]
			
			tmin.file = tttmin.files[which(substr(tttmin.files,nchar(tttmin.files)-8,nchar(tttmin.files)-7)==dayx)]
			
			### Now select the appropriate insolation file
			
			insol.file = insol.files[which(insol.vec==paste(monthx,dayx,sep=''))]
	
			### Create name of .sh file
	
			tname = paste(yearx,monthx,dayx,'.sh',sep='')
	
			### Create arguments
	
			arg.tmax.file = paste('tmax.file="',tmax.file,'" ',sep='') # Define the argument for tmax.file
	
			arg.tmin.file = paste('tmin.file="',tmin.file,'" ',sep='') # Define the argument for tmin.file
			
			arg.insol.file = paste('insol.file="',insol.file,'" ',sep='') # Define the argument for insol.file
	
			zz = file(tname,'w') # This command creates a file with the name 'tname' The 'w' command opens the file and writes the following lines of text into it
		
				cat('#!/bin/bash\n',file=zz) # First command line for HPC
				cat('cd $PBS_O_WORKDIR\n',file=zz) # Sets the directory to write out summary files at the above directory 'tmp.pbs'
				cat("R CMD BATCH --no-save --no-restore '--args ",arg.tmax.file,arg.tmin.file,arg.insol.file,"' ",script.file,' ',gsub('.sh','',tname),'.Rout \n',sep='',file=zz) # This line tells R not to save to the .Rdata file and also not to restore the environment from the .Rdata file, very important when batching or you will get multiple fuckups, also defines the arguments, says where script to run is located, and defines the name for the R.out file
				
			close(zz) # Closes the .sh file that you've just written
	
			# The follow line submits the job to the HPC, leave it commented out until you're ready to RUN all the jobs\
			# Always run the script first without removing the comment, this will still generate the .sh files for you to check before running
			# Once you're happy with that then submit a single .sh file and make sure it runs through to completion before batching out the rest with the following line
			
			system(paste('qsub -m n -l nodes=1:ppn=8 ',tname,sep=''))
			
			system(paste('sleep 1'))
			
			cat(paste(yearx,monthx,dayx,' - Submitted',sep=''),'\n')
			
			}
		
		
		if(paste(monthx,dayx,sep='')=='1231') 
		{cat('Waiting for 2 minutes before queueing files for the next 12 months\n')
		system(paste('sleep 120'))}
		
		}
		
	cat('###########################\n\n')	
	cat(paste(yearx,' - Completed',sep=''),'\n')
	
	}
	
# Close loop







